//
//  MyViewController.m
//  BP_NewClient
//
//  Created by letv on 15/5/15.
//  Copyright (c) 2015年 normal. All rights reserved.
//  我的-首页

#import "MyViewController.h"
#import "MyTableViewCell.h"
#import "BURequestManager.h"
#import "UserAccount.h"
#import "UIImageView+AFNetworking.h"
#import "BP_NewClient-Swift.h"
#import "AboutViewController.h"

@interface MyViewController (){
    NSArray *titleArray;
}

@end

@implementation MyViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self hideTabbar:NO];
    
    // 获取用户信息
    NSDictionary *dic = @{@"mobile":@"15011036823"};
    [[BURequestManager sharedRequest] requestPostWithParameterWithBlockDic:USER_INFO SetParamer:dic success:^(NSDictionary *responsDictionary){
        NSLog(@"responsDictionary is %@",responsDictionary);
      
        [userImg setImageWithURL1:[NSURL URLWithString:[responsDictionary objectForKey:@"image_url"]] placeholderImage:[UIImage imageNamed:@"14提交订单-门店"]];
        userName.text = [responsDictionary objectForKey:@"username"];
        
    } failure:^(NSString *errMsg){
        
    }];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self hideTabbar:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    titleArray = @[@"优惠券",@"我的收藏",@"关于我们",@"客服电话 024-2316336"];
    self.navigationItem.title = @"我的";
    [self addGetMoreFootView];
    [self setTableViewHeightWithTabBarhidden:NO];
    headerView.backgroundColor = [BPCommon colorWithHexValue:0xf7f7f7 alpha:1.0f];
    userImg.layer.masksToBounds = YES;
    userImg.layer.cornerRadius = 16;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(uploadImg:)];
    [userImg addGestureRecognizer:tap];

    
    UITapGestureRecognizer *tapBg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bgHeadAction:)];
    [headerView addGestureRecognizer:tapBg];
    
    integral.textColor = COLOR(0xfc8200);
    self._tableView.tableHeaderView = headerView;
    self._tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)bgHeadAction:(UITapGestureRecognizer *)recognizer{
    BPUserInfoViewController *userInfo = [[BPUserInfoViewController alloc] initWithNibName:@"BPUserInfoViewController" bundle:nil];
    [self.navigationController pushViewController:userInfo animated:YES];
}

- (void)uploadImg:(UITapGestureRecognizer *)recognizer{
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                             delegate:self
                                                    cancelButtonTitle:@"取消"
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:@"拍照",@"从相册获取", nil];
    [actionSheet showInView:self.view];
}

// 覆盖加载更多方法
- (void)addGetMoreFootView{
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return titleArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 54;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identy = @"MyTableViewCell";
    MyTableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:identy];
    NSArray *nibs;
    
    if (nil == cell) {
        nibs = [[NSBundle mainBundle]loadNibNamed:@"MyTableViewCell" owner:nil options:nil];
        cell = [nibs lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }
    if (indexPath.row < titleArray.count) {
        cell.title.text = OBJECT_OF_ATINDEX(titleArray,indexPath.row);
    }
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row == 0) {

    }else if (indexPath.row == 2){
        AboutViewController *about = [[AboutViewController alloc] init];
        [self.navigationController pushViewController:about animated:YES];
    }

}

#pragma -mark
#pragma -UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    if ([mediaType isEqualToString:@"public.image"]){
        [picker dismissViewControllerAnimated:NO completion:nil];
        
        UIImage *originImage = [info objectForKey:UIImagePickerControllerEditedImage];
        originImage = [BPCommon imageWithImageSimple:originImage scaledToSize:CGSizeMake(70, 70)];
        [BPCommon saveImage:originImage WithName:@"userImage.jpg"];

        NSString *filePath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"userImage.jpg"];
        NSFileManager *fileMag = [NSFileManager defaultManager];
        if ([fileMag fileExistsAtPath:filePath])
        {
            NSLog(@"filePath is %@",filePath);
            NSDictionary *dic = @{@"mobile":@"15011036823"};
            [[BURequestManager sharedRequest] requestLoadImgWihtPathAndImgPath:USER_UPLOAD_IMG setParamer:dic SetImgPath:filePath success:^(NSDictionary *dic){

                UpLoadImgModel *imgModel = [[UpLoadImgModel alloc] initWithDictionary:dic error:nil];
                if ([imgModel.error_code intValue] == 0) {
                    userImg.image = [UIImage imageWithContentsOfFile:filePath];
                }
                
            } failure:^(NSString *error){

            }];
            
        }
    }
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:NO completion:nil];
}


#pragma -mark
#pragma -mark UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    userPicker = [[UIImagePickerController alloc]init];
    userPicker.view.backgroundColor = [UIColor orangeColor];
    UIImagePickerControllerSourceType sourcheType;
    
    if (buttonIndex == 0) {
       sourcheType = UIImagePickerControllerSourceTypeCamera;
    }else{
       sourcheType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    userPicker.sourceType = sourcheType;
    userPicker.delegate = self;
    userPicker.allowsEditing = YES;
    [self presentViewController:userPicker animated:YES completion:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
